<template>
  <basic-container>
    <h3>存储</h3>
    <el-tag
      class="title"
      size="small"
    >基本读写删(持久化存储)</el-tag>
    <div class="box">
      <el-button
        type="primary"
        size="small"
        @click="setItem({name:'username', value:'avue'});"
      >set('username', 'avue')</el-button>

      <el-button
        type="success"
        size="small"
        @click="getItem({name:'username'});"
      >get('username')</el-button>

      <el-button
        type="danger"
        size="small"
        @click="delItem({name:'username'});"
      >remove('username')</el-button>
    </div>
    <el-tag
      class="title"
      size="small"
    >设置session(session存储)</el-tag>
    <div class="box">
      <el-button
        type="primary"
        size="small"
        @click="setItem({name:'username', value:'avue',type:'session'});"
      >set('username', 'avue')</el-button>

      <el-button
        type="success"
        size="small"
        @click="getItem({name:'username',type:'session'});"
      >get('username')</el-button>

      <el-button
        type="danger"
        size="small"
        @click="delItem({name:'username',type:'session'});"
      >remove('username')</el-button>
    </div>
    <el-tag
      class="title"
      size="small"
    >获取所有可以获得的数据</el-tag>
    <div class="box">
      <el-button
        type="success"
        size="small"
        @click="getAll()"
      >getAll(持久化存储)</el-button>
      <el-button
        type="success"
        size="small"
        @click="getAll({type:'session'})"
      >getAll(session存储)</el-button>
      <el-button
        type="danger"
        size="small"
        @click="clearAll()"
      >delAll(持久化存储)</el-button>
      <el-button
        type="danger"
        size="small"
        @click="clearAll({type:'session'})"
      >delAll(session存储)</el-button>
    </div>
    <avue-form
      :option="option"
      v-model="obj"
    >
      <template
        slot-scope="{row}"
        slot="input"
      >
        <el-tag>{{row}}</el-tag>
      </template>
    </avue-form>
  </basic-container>

</template>

<script>
import {
  setStore,
  getStore,
  removeStore,
  clearStore,
  getAllStore
} from "@/util/store";
export default {
  name: "store",
  data() {
    return {
      obj: {
        dynamic: [{
          input: 1,
          select: 1,
          radio: 1,
        }, {
          input: 2,
          select: 2,
          radio: 1,
        }]
      },
      option: {
        labelWidth: 110,
        labelPosition: 'top',
        column: [{
          label: '输入框',
          prop: "input1",
          span: 12,
          row: true
        },
        {
          label: '子表单',
          prop: 'dynamic',
          type: 'dynamic',
          span: 24,
          children: {
            align: 'center',
            headerAlign: 'center',
            rowAdd: (done) => {
              this.$message.success('新增回调');
              done({
                input: '默认值'
              });
            },
            rowDel: (row, done) => {
              this.$message.success('删除回调' + JSON.stringify(row));
              done();
            },
            column: [{
              width: 200,
              label: '输入框',
              prop: "input",
              formslot: true,
            }, {
              width: 200,
              label: '选择框',
              prop: "select",
              type: 'select',
              rules: [{
                type: 'number',
                required: true,
                message: '请选择选择框',
              }],
              dicData: [{
                label: '测试1',
                value: 1
              }, {
                label: '测试2',
                value: 2
              }]
            }, {
              width: 200,
              label: '多选',
              prop: "checkbox",
              type: 'checkbox',
              dicData: [{
                label: '测试1',
                value: 1
              }, {
                label: '测试2',
                value: 2
              }]
            }, {
              width: 200,
              label: '开关',
              prop: "switch",
              type: 'switch',
              dicData: [{
                label: '测试1',
                value: 1
              }, {
                label: '测试2',
                value: 2
              }]
            }, {
              width: 200,
              label: '数字框',
              prop: "number",
              type: 'number'
            }]
          }
        },

        ]
      }
    }
  },
  methods: {
    addAll() {
      for (let i = 0; i < 10; i++) {
        this.obj.dynamic.push({
          input: 1,
          select: 1,
          radio: 1,
        })
      }
    },
    setItem(params = {}) {
      const { name, value, type } = params;
      setStore({
        name: name,
        content: value,
        type: type
      });
      this.$message(`设置数据 ${name} = ${value}`);
    },
    getItem(params = {}) {
      const { name, type } = params;
      const content = getStore({
        name: name,
        type: type
      });
      this.$message(`获取数据 ${name} = ${content}`);
    },
    delItem(params = {}) {
      const { name, type } = params;
      removeStore({ name, type });
      this.$message(`删除数据 ${name}`);
    },
    clearAll(params = {}) {
      clearStore(params);
      this.$message(`清除全部数据完成`);
    },
    getAll(params = {}) {
      const list = getAllStore(params);
      console.log(list);
      this.$message(`结果已经打印到控制台`);
    }
  }
};
</script>

<style lang="scss">
.title {
  margin-bottom: 10px;
}
.box {
  margin-bottom: 20px;
}
</style>
